﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DC2010;
using DC2010.Objects;
using DC2010.Tools;
using DC2010.Objects.Info;
using DC2010_Editor.Formulars;
using DC2010_Editor.Properties;
using DC2010_Editor.Controls.Utils;
using DC2010.Objects.Base;

namespace DC2010_Editor.Controls
{
    public partial class ItemUC : UserControl
    {
        private Item data { get; set; }

        public ItemUC()
        {
            InitializeComponent();
        }

        private void ItemUC_Load(object sender, EventArgs e)
        {
            Refresh();
        }

        public void SetControls()
        {
            //Ids
            BasedOnTextBox.Text = data.Info.TemplateId;
            ItemIdTextBox.Text = data.Info.Id;


            inventoryTextBox2.SetRelatedObject(data.Info.LinkedObjects);
            HitImmaterialCheckBox.Checked = data.Info.HitImmaterial;
            checkBox4.Checked = data.Info.IsFloorItem;
            checkBox5.Checked = data.Info.IsWallItem;
            checkBox6.Checked = data.Info.IsCommonItem;

            textBox1.Text = data.Info.Weight.ToString();
            textBox3.Text = data.Info.ItemMessage;

            seTextBox1.SetRelatedObject(data.Info.PassiveEffects, EffectType.Pump);
            seTextBox2.SetRelatedObject(data.Info.ActiveEffects, EffectType.Instant);
            seTextBox3.SetRelatedObject(data.Info.UsageEffects, EffectType.Instant);
            seTextBox4.SetRelatedObject(data.Info.OnUseEffects, EffectType.Instant);


            inventoryTextBox1.SetRelatedObject(data.Info.ContainerItems.GetItems);

            comboBox3.SelectedItem = data.Info.ItemType;

            //Container
            checkBox2.Checked = data.Info.IsContainerItem;
            checkBox3.Checked = data.Info.IsContainerItemsDisplayed;
            comboBox2.SelectedItem = data.Info.ContainerType;

            scTextBox1.SetRelatedObject(data.Info.Conditions);
        }

        public void GetFromControls()
        {
            //Ids
            data.Info.TemplateId = BasedOnTextBox.Text;
            data.Info.Id = ItemIdTextBox.Text;

            data.Info.HitImmaterial = HitImmaterialCheckBox.Checked;
            data.Info.IsFloorItem = checkBox4.Checked;
            data.Info.IsWallItem = checkBox5.Checked;
            data.Info.IsCommonItem = checkBox6.Checked;

            data.Info.Weight = float.Parse(textBox1.Text);
            data.Info.ItemMessage = textBox3.Text;
            data.Info.ItemType = (ItemType)comboBox3.SelectedItem;

            //Container
            data.Info.IsContainerItem = checkBox2.Checked;
            data.Info.IsContainerItemsDisplayed = checkBox3.Checked;
            data.Info.ContainerType = (ItemType)comboBox2.SelectedItem;
        }

        private void crystalButton1_Click(object sender, EventArgs e)
        {
            Root.I.ItemSystem.LoadInstance();
            Refresh();
        }

        private void imageListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ImageListBox ilb = (ImageListBox)sender;

            if (ilb.SelectedItem != null)
            {
                if (ilb.SelectedItem is ImageListBoxItem)
                {

                    ImageListBoxItem ilbi = (ImageListBoxItem)ilb.SelectedItem;

                    if (ilbi.Tag is Item)
                    {
                        data = (Item)ilbi.Tag;
                        SetControls();

                        if (string.IsNullOrEmpty(data.Info.TemplateId)) pictureBox1.Image = Resources.tick;
                        if (!string.IsNullOrEmpty(data.Info.TemplateId))
                        {
                            if (Root.I.ItemSystem.Dict.ContainsKey(data.Info.TemplateId))
                                pictureBox1.Image = Resources.tick;
                            else
                                pictureBox1.Image = Resources.cross;
                        }
                    }
                }
            }

        }

        private void button10_Click(object sender, EventArgs e)
        {
            //noone was messing with that
            string id = string.Format(ItemIdTextBox.Text, Editor.I.ItemsCount);
            string tempId = BasedOnTextBox.Text;

            Item d = new Item();
            d.Info.Id = id;
            d.Info.TemplateId = tempId;

            Root.I.ItemSystem.AddToSystem(d, id, ObjectTarget.Core);
            Root.I.ItemSystem.Save();

            Refresh();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            ChooseBasedOn chbo = new ChooseBasedOn("Item");
            chbo.ShowDialog();

            if (!string.IsNullOrEmpty(chbo.ChosenAncessor))
            {
                Item i = Root.I.IS.GetItem(chbo.ChosenAncessor);
                Item iNew = (Item)i.Clone();

                string id = string.Format(ItemIdTextBox.Text+"_{0}", Editor.I.ItemsInstanceCount);
                string tempId = BasedOnTextBox.Text;


                iNew.Info.Id = id;
                iNew.Info.TemplateId = i.Info.Id;

                Root.I.ItemSystem.AddToSystem(iNew, id, ObjectTarget.Instance);
                Root.I.ItemSystem.SaveInstance();

                Refresh();
            }

        }

        private void button7_Click(object sender, EventArgs e)
        {
            GetFromControls();

            Root.I.ItemSystem.Save();
            Root.I.ItemSystem.SaveInstance();

            Refresh();
        }

        private void Refresh()
        {
            TemplatesImageListBox.Items.Clear();
            DungeonImageListBox.Items.Clear();


            comboBox2.DataSource = null;
            comboBox3.DataSource = null;
            comboBox2.Items.Clear();
            comboBox3.Items.Clear();

            foreach (Item i in Root.I.ItemSystem.List)
            {
                Color clr = Color.Black;
                if (!i.Validate()) clr = Color.Red;
 
                ImageListBoxItem itm = new ImageListBoxItem(i.Info.Id, Resources.Core, i,clr);
                TemplatesImageListBox.Items.Add(itm);
            }

            foreach (Item i in Root.I.ItemSystem.ListInstance)
            {
                BaseObject c = i.FindOwner();
                if (c == null)
                {
                    Color clr = Color.Black;
                    if (!i.Validate()) clr = Color.Red;

                    ImageListBoxItem itm = new ImageListBoxItem(i.Info.Id, Resources.Instanced, i, clr);
                    DungeonImageListBox.Items.Add(itm);
                }
                else
                {
                    ImageListBoxItem itm = new ImageListBoxItem(i.Info.Id, Resources.InstancedWithOwner, i);
                    DungeonImageListBox.Items.Add(itm);
                }


            }

            comboBox2.DataSource = Enum.GetValues(typeof(ItemType));
            comboBox3.DataSource = Enum.GetValues(typeof(ItemType));

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                ItemGfxForm igfxf = new ItemGfxForm(data.Gfx);
                igfxf.ShowDialog();
            }
        }

        private void crystalButton5_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                Root.I.ItemSystem.MoveToC(data.Info.Id);
            }

            Refresh();
        }

        private void crystalButton6_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                Root.I.ItemSystem.MoveToI(data.Info.Id);
            }

            Refresh();
        }

        private void crystalButton7_Click(object sender, EventArgs e)
        {
            if (data != null)
            {
                data.Id = data.Info.Id;
                Root.I.ItemSystem.RemoveFromSystem(data, ObjectTarget.Core);
                Root.I.ItemSystem.RemoveFromSystem(data, ObjectTarget.Instance);
            }

            Refresh();
        }

        private void crystalButton8_Click(object sender, EventArgs e)
        {
            /// Remove from system.
            /// 

            data.Id = data.Info.Id;
            Root.I.ItemSystem.RemoveFromSystem(data, ObjectTarget.Instance);

            Item i = Root.I.IS.GetItem(data.Info.TemplateId);
            Item iNew = (Item)i.Clone();

            string id = data.Info.Id;
            string tempId = BasedOnTextBox.Text;

            iNew.Info.Id = id;
            iNew.Info.TemplateId = i.Info.Id;

            Root.I.ItemSystem.AddToSystem(iNew, id, ObjectTarget.Instance);
            Root.I.ItemSystem.SaveInstance();

            Refresh();
        }

    }
}
